#include "hc05_task.h"
#include "oled.h"
#include "delay.h"
#include "uart_data_class.h"
#include "buzzer.h"
#include "flash_uid.h"
#include <string.h>

#if DEBUG

#include "debug_uart.h"

#endif


extern UART_IO_RECV uartio_buf;
uint8_t hc05_recv_flag = 0;
UART_ENUM_RECV flag_uart_s = WAIT_HEAD;
UART_HCRECV_DATATYPE hc05_recv;

//wifi变量
uint8_t set_wifiname_flag = 0;
uint8_t set_wifipassword_flag = 0;
uint8_t wifi_buf[64];
uint8_t wifi_len = 0;



extern uint8_t rfid_schema_flag;
extern uint8_t uid_handle_flag;//加减卡标志 0 常态 1加卡 2减卡
extern uint8_t uid_weigen_flag;//韦根模式标志
//任务函数
void hc05_task(void)
{
	uart_recv_com();
	if(1 == hc05_recv_flag)
	{
		switch(hc05_recv.com)
		{
			case 'A'://开门命令
			{
				hc05_send_str("unlock ok!");
        si12_succced();
				break;
			}
			case 'B'://退出加减卡命令
			{
				uid_handle_flag = 0;
				uid_weigen_flag = 0;
				hc05_send_str("Exit card status ok!");
				rfid_schema_flag = 0;
				oled_rfid_mode(rfid_schema_flag);
				break;
			}
			case 'C'://加卡命令
			{
				uid_handle_flag = 1;
				uid_weigen_flag = 0;
				rfid_schema_flag = 1;//rfid 加卡
				oled_rfid_mode(rfid_schema_flag);
				hc05_send_str("Add card status ok!");
				break;
			}
			case 'D'://减卡命令
			{
				uid_handle_flag = 2;
				uid_weigen_flag = 0;
				rfid_schema_flag = 2;//rfid 减卡
				oled_rfid_mode(rfid_schema_flag);
				hc05_send_str("Subtract card status ok!");
				break;
			}
      case 'E'://设置wifi名
      {
        set_wifiname_flag = 1;
        set_wifipassword_flag = 0;
        uartio_buf.head = uartio_buf.tail = 0;
        hc05_send_str("wifi name");
        break;
      }
      case 'F'://设置wifi名
      {
        set_wifiname_flag = 0;
        set_wifipassword_flag = 1;
        uartio_buf.head = uartio_buf.tail = 0;
        hc05_send_str("wifi password");
        break;
      }
		}
		hc05_recv_flag = 0;
	}
  else if(hc05_recv_flag == 2 || hc05_recv_flag == 3)//设置wifi
  {
    delay_ms(64);
    uart_recv_com();
    
    if(hc05_recv_flag == 2)//存wifi名
    {
      #if DEBUG
      
      UsartPrintf(USART_DEBUG, "wifi name:%s\r\n",wifi_buf);
      
      #endif
      storage_wifi_name(wifi_buf,wifi_len);
      
      #if DEBUG
      uint8_t i;
      uint8_t buffer;
      UsartPrintf(USART_DEBUG, "name: \r\n");
      for(i=0;i<34;i++)
      {
        buffer = flash_read_byte(WIFINAME_ADDRESS_SIZE+i);
        UsartPrintf(USART_DEBUG, "%02x ",buffer);
      }
      UsartPrintf(USART_DEBUG, "\r\n");
      #endif
      
    }
    else  //存wifi密码
    {
      #if DEBUG
      
      UsartPrintf(USART_DEBUG, "wifi password:%s\r\n",wifi_buf);
      
      #endif
      
      storage_wifi_password(wifi_buf,wifi_len);
      #if DEBUG
      uint8_t i;
      uint8_t buffer;
      UsartPrintf(USART_DEBUG, "password: \r\n");
      for(i=0;i<34;i++)
      {
        buffer = flash_read_byte(WIFIPASSWORD_ADDRESS_SIZE+i);
        UsartPrintf(USART_DEBUG, "%02x ",buffer);
      }
      UsartPrintf(USART_DEBUG, "\r\n");
      #endif
    }
    set_wifiname_flag = 0;
    set_wifipassword_flag = 0;
    uartio_buf.head = uartio_buf.tail = 0;
    memset(wifi_buf,0,wifi_len);
    wifi_len = 0;
    hc05_recv_flag = 0;
  }
}

void uart_recv_com(void)
{
	uint8_t data;
	uint8_t len,i,bcc;
  
	while(uartio_buf.head != uartio_buf.tail)
	{
		data = uartio_buf.uart_io_buf[uartio_buf.tail++];
		uartio_buf.tail %= 128;
    #if DEBUG
    
//        UsartPrintf(USART_DEBUG, "%02x\r\n ",data);
    delay_ms(1);
    #endif
    

    if(set_wifiname_flag == 1 || set_wifipassword_flag == 1)
    {
      
      if(set_wifiname_flag == 1)
      {
        hc05_recv_flag = 2;
      }
      else
      {
        hc05_recv_flag = 3;
      }
      wifi_buf[wifi_len++] = data;
    }
    else
    {
      switch(flag_uart_s)
      {
        case WAIT_HEAD:
        {
          if(data == 0xAA)
          {
            flag_uart_s = WAIT_ADDR;
            hc05_recv.head = data;
          }
          break;
        }
        case WAIT_ADDR:
        {
          if(data == 0x02)
          {
            flag_uart_s = WAIT_COM;
            hc05_recv.addr = data;
          }
          else flag_uart_s = WAIT_HEAD;
          break;
        }
        case WAIT_COM:
        {
          flag_uart_s = WAIT_DATALEN;
          hc05_recv.com = data;
          break;
        }
        case WAIT_DATALEN:
        {
          if(data<=0)
          {
            flag_uart_s = WAIT_TAIL;
          }
          else if(data>8)
          {
            flag_uart_s = WAIT_HEAD;
          }
          else
          {
            flag_uart_s = WAIT_DATA;
            len = 0;
          }
          hc05_recv.datalen = data;
          break;
        }
        case WAIT_DATA:
        {
          hc05_recv.data[len++] = data;
          if(len == hc05_recv.datalen)flag_uart_s = WAIT_TAIL;
          break;
        }
        case WAIT_TAIL:
        {
          if(data == 0xBB)
          {
            hc05_recv.tail = data;
            flag_uart_s = WAIT_BCC;
            bcc = 0;
          }
          else flag_uart_s = WAIT_HEAD;
          break;
        }
        case WAIT_BCC:
        {
          bcc ^= hc05_recv.head;
          bcc ^= hc05_recv.addr;
          bcc ^= hc05_recv.com;
          bcc ^= hc05_recv.datalen;
          for(i=0;i<hc05_recv.datalen;i++)
          {
            bcc ^= hc05_recv.data[i];
          }
          bcc ^= hc05_recv.tail;
          if(bcc == data)
          {
            hc05_recv.bcc = data;
            hc05_recv_flag = 1;
            flag_uart_s = WAIT_HEAD;
            uartio_buf.head = 0; 
            uartio_buf.tail = 0;
            return;
          }
          else flag_uart_s = WAIT_HEAD;
          break;
        }
        case WALT_STA:
        {
          break;
        }
      }
    }
	}
}
